package com.hunktimes.wholesale.ui.login

import android.os.Bundle
import android.view.View
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BaseRootFragment
import com.hunktimes.wholesale.databinding.FragmentLoginBinding
import com.hunktimes.wholesale.util.ToastUtil
import com.hunktimes.wholesale.vm.LoginVM

/**
 * Author: Delete
 * Date: 2020/7/22 15:45
 * Description:
 *
 */
class LoginFragment : BaseRootFragment(R.layout.fragment_login) {
    override val titleRes: Int
        get() = R.string.title_login

    lateinit var viewBinding: FragmentLoginBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = FragmentLoginBinding.bind(view)
    }

    val vm by viewModels<LoginVM>()
    override fun onStart() {
        super.onStart()
        viewBinding.regTv.setOnClickListener {
            rootNavController.navigate(R.id.action_navigation_login_to_navigation_register)
        }
        viewBinding.forgetTv.setOnClickListener {
            rootNavController.navigate(R.id.action_navigation_login_to_navigation_forget_pwd)
        }

        viewBinding.userPwdEt.doOnTextChanged { text, _, _, _ -> }
        viewBinding.confirmBtn.setOnClickListener {
            val userName = vm.userName.value ?: ""
            if (userName.isBlank()) {
                ToastUtil.showErrorToast("请输入用户名")
                return@setOnClickListener
            }
            val pwd = vm.pwd.value ?: ""
            if (pwd.isBlank()) {
                ToastUtil.showErrorToast("请输入密码")
                return@setOnClickListener
            }
            vm.login()
        }
    }
}